<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>评论列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="10">
        <el-col :span="6">
          <!-- 搜索框 -->
          <el-input
            placeholder="用户id"
            v-model="queryInfo.query"
            clearable
            @clear="getCommentList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getCommentList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <!-- 表单 -->
      <el-table
        ref="multipleTable"
        :data="commentList"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
      >
        <el-table-column prop="commentid" label="评论id"> </el-table-column>
        <el-table-column prop="userid" label="用户id"> </el-table-column>
        <el-table-column prop="orderid" label="订单id"></el-table-column>
        <el-table-column prop="content" label="评论内容"> </el-table-column>
        <el-table-column prop="createtime" label="评论时间"> </el-table-column>
        <!-- 这里指定了一个宽度，才能撑下三个按钮 -->
        <el-table-column
          prop="address"
          label="操作"
          show-overflow-tooltip
          width="280px"
        >
          <!-- 这里也是使用了 插槽 -->
          <template slot-scope="scope">
            <el-button type="success" round @click="goDetail(scope.row)"
              >详情</el-button
            >
            <el-button type="primary" round @click="reply(scope.row)"
              >回复评论</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 3, 5]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 弹出框 -->
    <el-dialog title="评论详情" :visible.sync="commentDialogVisible">
      <!-- 基础信息 -->
      <el-divider content-position="left">评价信息</el-divider>
      <el-descriptions v-if="row != null">
        <el-descriptions-item label="总体评分">
          <el-rate v-model="row.rate" disabled> </el-rate>
        </el-descriptions-item>
        <el-descriptions-item label="评价日期">{{
          row.createtime
        }}</el-descriptions-item>
        <el-descriptions-item label="评价">{{
          row.content
        }}</el-descriptions-item>
      </el-descriptions>

      <el-descriptions v-if="row == null">
        <el-descriptions-item label="总体评分"> 暂无评价 </el-descriptions-item>
      </el-descriptions>

      <!-- 评论的图片 -->
      <el-carousel>
        <el-carousel-item v-for="(item, i) in row.commenturl" :key="i">
          <!-- <h3 class="small">{{ item }}</h3> -->
          <el-image :src="item"></el-image>
        </el-carousel-item>
      </el-carousel>
      <!-- 回复的内容 -->
      <el-divider content-position="left">回复内容</el-divider>
      <el-tag
        :key="item.replyid"
        v-for="item in replydata2"
        closable
        :disable-transitions="false"
        @close="handleClose(item.replyid)"
        type="warning"
      >
        {{ item.text }}
      </el-tag>
    </el-dialog>

    <!-- 添加信息的对话框 -->
    <el-dialog
      title="添加回复内容"
      :visible.sync="replyDialogVisible"
      @close="replyDialogClosed"
    >
      <el-form :model="replydata" ref="replyFormRef" label-width="100px">
        <el-form-item label="回复内容" prop="replycontent">
          <el-input v-model="replydata.replycontent"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="replyDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSuccess">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        //当前的页数
        pagenum: 1,
        //当前每页显示多少条数据
        pagesize: 5,
      },
      commentList: [],
      row: [],
      commentDialogVisible: false,
      replyDialogVisible: false,
      replydata: {
        replycontent: "",
      },
      total: 0,
      commentRow: {},
      replydata2: [],
    };
  },
  created() {
    this.getCommentList();
  },
  methods: {
    async getCommentList() {
      const { data: res } = await this.$http.post(
        `/comment/page/${this.queryInfo.pagenum}/${this.queryInfo.pagesize}`,
        {
          userid: this.queryInfo.query,
        }
      );
      console.log(res, "评论列表");
      this.commentList = res.data.records;
      console.log(this.commentList);
      this.total = res.data.total;
    },
     goDetail(row) {
      //拿到用户的信息
      let key = "commenturl";
      let commenturl = JSON.parse(row.url);
      row[key] = commenturl;
      this.row = row;
      console.log(this.row);
      this.getReplyData()
      this.commentDialogVisible = true;
    },
    //获取回复信息
    async getReplyData(){

      //获取到该评论的所有回复
      const { data: res } = await this.$http.get(
        `/reply/getByCommentid/${this.row.commentid}`
      );
      console.log(res, "回复列表");
      this.replydata2 = res.data;


    },

    async handleClose(id) {
      const { data: res } = await this.$http.delete(`/reply/delete/${id}`);
      console.log(res, "删除标签");
      if (res.flag == true) {
        this.$message.success("删除成功");
        this.getReplyData()
      } else {
        return this.$message.error("删除失败");
      }
    },
    reply(row) {
      this.replyDialogVisible = true;
      this.commentRow = row;
    },
    async addSuccess() {
      const { data: res } = await this.$http.post("/reply/add", {
        commentid: this.commentRow.commentid,
        adminid: JSON.parse(window.sessionStorage.getItem("adminInfo")).adminId,
        text: this.replydata.replycontent,
      });
      console.log(res, "添加回复之后的返回");
      if (res.flag == true) {
        this.$message.success("添加成功");
        this.replyDialogVisible = false;
      } else {
        return this.$message.error("添加失败");
      }
    },
    replyDialogClosed() {
      this.$refs.replyFormRef.resetFields()
    },
    //监听 pageesize 改变的事件
    handleSizeChange(newSize) {
      console.log(`每页 ${newSize} 条`);
      this.queryInfo.pagesize = newSize;
      this.getCommentList();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(`当前页: ${newPage}`);
      this.queryInfo.pagenum = newPage;
      this.getCommentList();
    },
  },
};
</script>

<style scoped>
.el-tag {
  margin: 7px 7px;
}
</style>